//
//  HSAwardDetailVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/1.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSAwardDetailVC.h"
#import "HSAwardDetailModel.h"
#import "HSAwardDetailCell.h"

@interface HSAwardDetailVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HSAwardDetailModel *detailModel;
@property (nonatomic, strong) HSSubInfoModel *model;

@end

@implementation HSAwardDetailVC

- (instancetype)initWithModel:(HSSubInfoModel *)model
{
    if (self = [super init]) {
        _model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.model.title;
    [self.view addSubview:self.tableView];
    @weakify(self);
    [self.hintView setReloadBlock:^{
        @strongify(self);
        self.hintView.hintState = MYHintStateLoading;
        [self refresh];
    }];
    self.hintView.reloadBlock();
}

- (void)refresh
{
    if (!self.model || !self.model.id || !self.model.prizeInfo.type) {
        self.hintView.hintState = MYHintStateNoData;
        return;
    }
    NSString *url = @"http://www.59cp.com/api/Games/getHistoryNums";
    NSDictionary *param = @{@"id" : self.model.id,
                            @"type" : self.model.prizeInfo.type,
                            @"current_page" : @1,
                            @"page_count" : @30};
    @weakify(self);
    [[MYHttpHelper shareHelper] getWithUrl:url params:param success:^(NSDictionary *dict) {
        @strongify(self);
        self.hintView.hintState = MYHintStateHidden;
        self.detailModel = [HSAwardDetailModel modelWithDictionary:dict];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        self.hintView.hintState = MYHintStateNoData;
        [UIWindow showError:error];
    }];
}

#pragma mark - UITableView Delegate & Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.detailModel.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseId = @"HSAwardDetailCell";
    HSAwardDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (!cell) {
        cell = [[HSAwardDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId isKuai3:self.model.prizeInfo.isKuai3];
    }
    cell.model = self.detailModel.data[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CPListType == 1 ? 70 : 90;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - NavBarHeight)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

@end
